<template>
  <el-card>
    <!-- 头部组件 -->
    <PageTools :total="total">
      <template #left>
        <el-form ref="form" :model="formData" :inline="true">
          <el-form-item>
            <el-input v-model="queryData.title" placeholder="根据用户名搜索" />
          </el-form-item>
          <el-form-item>
            <el-button size="mini" @click="enSubmit">清除</el-button>
            <el-button size="mini" type="primary" @click="submit">搜索</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template #right>
        <el-button
          size="mini"
          type="success"
          icon="el-icon-edit"
          style="height: 100%"
          @click="addHandle"
        >新增权限组</el-button>
      </template>
    </PageTools>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column type="selection" />
      <el-table-column label="用户名" prop="title" />
      <el-table-column sortable label="日期" prop="create_date" />
      <el-table-column label="操作">
        <template slot-scope="{row}">
          <el-button circle plain type="primary" @click="editHandle(row.id)">
            <i class="el-icon-edit" />
          </el-button>
          <el-button circle plain type="danger" @click="delHandle(row.id)">
            <i class="el-icon-delete" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row type="flex" justify="end">
      <el-pagination
        background
        :current-page="queryData.page"
        :page-sizes="[2, 3, 10]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-row>
    <!-- 添加，编辑弹窗 -->
    <add-permission ref="addPermission" @update="initDate" />
  </el-card>
</template>

<script>
import { permissionsList, delPermissions } from '@/api/permissions'
import PageTools from '@/components/PageTools'
import AddPermission from './components/addPermission.vue'
export default {
  components: {
    PageTools,
    AddPermission
  },
  data() {
    return {
      formData: {},
      queryData: {
        page: 1,
        pagesize: 2,
        title: ''
      },
      list: [],
      total: 0
    }
  },
  created() {
    this.initDate()
  },
  methods: {
    async initDate() {
      const { counts, list } = await permissionsList(this.queryData)
      this.total = counts
      list.map((item) => {
        item.create_date = new Date(item.create_date).toLocaleString()
        return item
      })
      this.list = list
    },
    handleSizeChange(val) {
      this.queryData.pagesize = val
      this.initDate()
    },
    handleCurrentChange(val) {
      this.queryData.page = val
      this.initDate()
    },
    enSubmit() {
      this.queryData.title = ''
      this.initDate()
    },
    submit() {
      this.initDate()
    },
    addHandle() {
      this.$refs.addPermission.dialogVisible = true
    },
    editHandle(id) {
      this.$refs.addPermission.dialogVisible = true
      this.$refs.addPermission.getPermissions(id)
    },
    async delHandle(id) {
      await this.$confirm('确定删除吗')
      await delPermissions(id)
      this.$message.success('删除成功')
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initDate()
    }
  }
}
</script>

<style scope>
</style>
